class Product
  def inc_version_cpp( ver_file )
    #version array (like: [1,9,1678,0])
    va = []

    File.open(ver_file){|f|
      while str = f.gets
        if str.match(/PRODUCT_VERSION_STRING\s+\"(\d+)\.(\d+)\.(\d+)\.(\d+)\"/)
          va = [$1, $2, $3, $4]
          va = va.collect{|v| v.to_i}
          break
        end
      end
    }

    raise "something wrong with version: #{va.inspect}" if (va == []) or (va.select{|v| !(v.kind_of? Numeric) }.size > 0)

    va[2] += 1

    File.chmod 0777, ver_file
    macro_name = "__" + File.basename(ver_file).gsub(/\./, '_').upcase + "__"

    File.open(ver_file, 'w'){|f|
      f.puts(
        "//--------------------------------------------------------------------------\n" +
        "//  Header with product version information.\n" +
        "//\n" +
        "//  Include it in your Version.rc and use PRODUCT_VERSION macro\n" +
        "//  as version info string.\n" +
        "//--------------------------------------------------------------------------\n" +
        "#ifndef #{macro_name}\n" +
        "#define #{macro_name}\n" +
        "\n\n" +
        "#define PRODUCT_VERSION_STRING \"#{va.join('.')}\"\n" +
        "#define PRODUCT_VERSION_MAJOR #{va[0]}\n" +
        "#define PRODUCT_VERSION_MINOR #{va[1]}\n" +
        "#define PRODUCT_VERSION_BUILD #{va[2]}\n" +
        "#define PRODUCT_VERSION_REVISION #{va[3]}\n" +
        "#define BUILD_START_TIME \"#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}\"\n" + # TODO: надо не Тайм.нау, а начало билда. потом подумаю
        "#define PRODUCT_NAME_STRING \"#{@product_name}\"" +
        "\n\n" +
        "#endif // #{macro_name}\n"
      )
    }
    puts "Setting version to #{va.join('.')}"

    @version = va
  end

  def inc_version_lua( ver_file )
    #version array (like: [1,1678])
    va = []

    File.open(ver_file){|f|
      while str = f.gets
        if str.match(/PRODUCT_VERSION_STRING\s+=\s+\"(\d+)\.(\d+)\"/)
          va = [$1, $2]
          va = va.collect{|v| v.to_i}
          break
        end
      end
    }

    raise "something wrong with version: #{va.inspect}" if (va == []) or (va.select{|v| !(v.kind_of? Numeric) }.size > 0)
    va[1] += 1

    File.chmod 0777, ver_file

    File.open(ver_file, 'w'){|f|
      f.puts(
        "-- version file for build-machine\n" +
        "-- !!!DO NOT CHANGE ANYTHING!!!\n" +
        "\n" +
        "PRODUCT_VERSION_STRING = \"#{va.join('.')}\"\n" +
        "PRODUCT_VERSION_MAJOR = #{va[0]}\n" +
        "PRODUCT_VERSION_MINOR = #{va[1]}\n" +
        "BUILD_START_TIME = \"#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}\"\n"
      )
    }

    puts "Setting version to #{va.join('.')}"
    @version = va
  end
end
